//
//  AY_AudioMuLuView.m
//  AiYue
//
//  Created by 姜栋梁 on 2023/11/27.
//

#import "AY_AudioMuLuView.h"
#import "AY_DirectoryCell.h"

@interface AY_AudioMuLuView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *gesView;
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UIButton *cancelBtn;
@property (nonatomic,strong) UILabel *chapterLabel;
@property (nonatomic,strong) UILabel *sortLabel;
@end

@implementation AY_AudioMuLuView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.gesView];
        [self addSubview:self.backView];
        [self.backView addSubview:self.tableView];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"AY_DirectoryCell";
    AY_DirectoryCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[AY_DirectoryCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.chapterModel = self.dataSource[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YNUM(44);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ReadChapterModel *model = self.dataSource[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(didSelectMuluWithChapterIndex:)]) {
        [self.delegate didSelectMuluWithChapterIndex:model.sortNo.integerValue - 1];
    }
}

- (void)setUI {
    self.chapterLabel = [AYTool createLabelWithFrame:CGRectZero andText:[NSString stringWithFormat:@"共%ld章", self.dataSource.count] andTextColor:COLOR(@"#97A4B0") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(14)] andLines:1 andRadius:0 andAddSubView:self.backView];
    [self.chapterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.backView).offset(YNUM(16));
    }];
    
    
    self.sortLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"正序" andTextColor:COLOR(@"#FD8301") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(14)] andLines:1 andRadius:0 andAddSubView:self.backView];
    [self.sortLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.backView).offset(YNUM(-16));
        make.centerY.equalTo(self.chapterLabel);
    }];
    
    UIImageView * sortImgView = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"AY_sort.png") andBg:[UIColor clearColor] andRadius:0.0 andAddSubView:self.backView];
    sortImgView.userInteractionEnabled = YES;
    [sortImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(YNUM(20));
        make.width.mas_equalTo(YNUM(20));
        make.right.equalTo(self.sortLabel.mas_left).offset(YNUM(-2));
        make.centerY.equalTo(self.chapterLabel);
    }];
    
    UIButton * sortBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:nil andFont:nil andTitleColor:[UIColor clearColor] andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(sortWithAction:) andRadius:YNUM(8) andAddSubView:self.backView];
    [sortBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.chapterLabel);
        make.right.equalTo(self.backView);
        make.height.mas_offset(YNUM(44));
        make.width.mas_offset(YNUM(45));
    }];
    
    UIView *footerV = [[UIView alloc]initWithFrame:CGRectMake(0, YNUM(340), SCREEN_W, YNUM(60))];
    footerV.backgroundColor = [UIColor whiteColor];
    UIButton * sureBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"关闭" andFont:[UIFont boldSystemFontOfSize:16] andTitleColor:[UIColor whiteColor] andBg:THEME_COLOR andImage:nil andBgImage:nil andTarget:self andAction:@selector(dismiss) andRadius:YNUM(8) andAddSubView:footerV];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(footerV).offset(YNUM(-16));
        make.left.equalTo(footerV).offset(YNUM(16));
        make.right.equalTo(footerV).offset(YNUM(-16));
        make.height.mas_offset(YNUM(44));
    }];
    [self.backView addSubview:footerV];
}

- (void)setDataSource:(NSMutableArray *)dataSource {
    _dataSource = dataSource;
    [self setUI];
    [self.tableView reloadData];
}

#pragma mark - 排序
- (void)sortWithAction:(UIButton *)btn {
    if (!self.dataSource || [self.dataSource count] <= 0) {
        return;
    }
    btn.selected = !btn.selected;
    if (btn.selected) {
        self.sortLabel.text = @"倒序";
        self.dataSource = [[self.dataSource reverseObjectEnumerator] allObjects];
        [self.tableView reloadData];
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }else {
        self.sortLabel.text = @"正序";
        self.dataSource = [[self.dataSource reverseObjectEnumerator] allObjects];
        [self.tableView reloadData];
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }
}

- (void)showMuLuView:(UIView *)view {
    self.frame = view.bounds;
    [view addSubview:self];
    [UIView animateWithDuration:0.3f animations:^{
        self.backView.frame = CGRectMake(0, YNUM(400), SCREEN_W, SCREEN_H - YNUM(400));
    }];
}

- (void)dismiss {
    [UIView animateWithDuration:0.3f animations:^{
        self.backView.frame = CGRectMake(0, SCREEN_H, SCREEN_W, SCREEN_H - YNUM(400));
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)ges:(UITapGestureRecognizer *)ges
{
    [self dismiss];
}

- (UIView *)backView {
    if (!_backView) {
        _backView = [AYTool createViewWithFrame:CGRectMake(0, SCREEN_H, SCREEN_W, SCREEN_H - YNUM(400)) andBg:[UIColor whiteColor] andRadius:0 andAddSubView:self];
    }
    return _backView;
}

-(UIView *)gesView
{
    if (!_gesView) {
        _gesView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
        _gesView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3f];
        [_gesView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ges:)]];
    }
    return _gesView;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, YNUM(35), SCREEN_W, SCREEN_H - YNUM(400) - GK_TABBAR_HEIGHT - YNUM(35)) style:UITableViewStylePlain];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.dataSource = self;
        tableView.translatesAutoresizingMaskIntoConstraints = NO;
        tableView.backgroundColor = [UIColor whiteColor];
        tableView.delegate = self;
        tableView.tableFooterView = [[UIView alloc] init];
        _tableView = tableView;
    }
    return _tableView;
}
@end
